package com.edu.controller;

import com.edu.bean.User;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/session")
public class SessionSafety {

//    @RequestMapping("session1")
//    public String session1(User user, HttpSession session){
//        System.out.println(user);
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println(user);
//        session.setAttribute("user",user);
//        return "forward:/index2.jsp";
//    }

//    public String username;
//
//    @RequestMapping("/session2")
//    public String session1(String username, HttpServletRequest request){
//        this.username = username;
//        System.out.println(this.username);
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println(this.username);
//        request.setAttribute("username",this.username);
//        return "forward:/index3.jsp";
//    }

//    @Resource
//    private HttpSession session;
//
//    @RequestMapping("session3")
//    public String session3(User user) {
//        System.out.println(user);
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println(user);
//        session.setAttribute("user", user);
//        return "forward:/index2.jsp";
//    }

    private HttpSession session;

    @ModelAttribute
    public void init(HttpSession session){
        this.session = session;
    }

    @RequestMapping("session4")
    public String session4(User user) {
        System.out.println(user);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(user);
        session.setAttribute("user", user);
        return "forward:/index2.jsp";
    }

}
